<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <label><h1>添加博客或者修改</h1></label>
    <table>
        <tr>
            <td><label for="">标题：</label></td>
            <td><input type="text" placeholder="请输入商品名称" name="StudentName"></td>
        </tr>
        <tr>
            <td><label for="">摘要：</label></td>
            <td><input type="text" placeholder="请输入售价" name="Studentclass"></td>
        </tr>
        <tr>
            <td><label for="">内容：</label></td>
            <td><input type="text" placeholder="请输入库存数量" name="Studentipone"></td>
        </tr>
        <tr>
            <td><label for="">作者：</label></td>
            <td><input type="text" placeholder="请输入供货商" name="Studentaddress"></td>
        </tr>
        <tr>
            <td><input type="button" value="保存" onclick="save()"></td>
            <td><input type="button" value="取消" onclick="cancel()"></td>
        </tr>
    </table>
    <script src="./js/jquery-3.6.0.min.js"></script>
    <script src="./config/baseUrl.js"></script>
    <!-- <script src="./config/baseUrl.js"></script> -->
    <script src="./js/api.js"></script>
    <script src="./js/init.js"></script>
    <script>
        $(function () {
            let id = getUrlParam('id');
            id = id || 0;
            console.log(id);
            getProductById(id).then(res => {
                console.log(res);
                let obj = res.data;
                $('[name=StudentName]').val(obj.StudentName);
                $('[name=Studentclass]').val(obj.Studentclass);
                $('[name=Studentipone]').val(obj.Studentipone);
                $('[name=Studentaddress]').val(obj.Studentaddress);
            })
        })
        function getUrlParam(name) {

            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); 

            var r = window.location.search.substr(1).match(reg); 

            if (r != null) return unescape(r[2]); return null; 

        }
        function save() {
            let id = getUrlParam('id');
            console.log(id);
            let obj = {
                productName: $('[name=StudentName]').val(),
                price: $('[name=Studentclass]').val(),
                stock: $('[name=Studentipone]').val(),
                supplier: $('[name=Studentaddress]').val(),
            }
            if (id) {
                updateProduct(id, obj).then(res => {
                    console.log(res);
                    location.href = './index.html';
                })
            } else {
                let params = {
                    StudentName: obj.StudentName,
                    Studentclass: obj.pricStudentclasse,
                    Studentipone: obj.Studentipone,
                    Studentaddress: obj.Studentaddress
                }
                addProduct(params).then(res => {
                    location.href = './index.html';
                })
            }


        }
        function cancel() {
            location.href = './index.html';
        }
    </script>
</body>

</html>